<template>
  <div>
    <el-drawer
      v-if="visible"
      class="orderDetail"
      :visible.sync="visible"
      :with-header="false"
      :append-to-body="true"
      size="1200px"
    >
      <div class="title">
        <div class="title-text">调拨计划详情（{{ detail.planCode }}）</div>
        <el-tag :type="getType(detail.status)" class="tag">{{ detail.status ? formatColumnMap(statusMap, 'value', detail.status, 'desc') : '' }}</el-tag>
      </div>

      <!-- 左右布局 -->
      <div class="center">
        <!-- 左侧 -->
        <div class="left">
          <div class="sub-title">基本信息</div>
          <el-descriptions size="medium" :label-style="labelStyle" :content-style="contentStyle">
            <el-descriptions-item label="调拨计划单号">{{ detail.planCode }}</el-descriptions-item>
            <el-descriptions-item label="起点站">{{ detail.startOrgName }}</el-descriptions-item>
            <el-descriptions-item label="终点站">{{ detail.endOrgName }}</el-descriptions-item>
            <el-descriptions-item label="创建时间">{{ detail.createTime ? parseTime(detail.createTime, '{y}-{m}-{d} {h}:{i}:{s}') : '' }}</el-descriptions-item>
            <el-descriptions-item label="创建人">{{ detail.createUser }}</el-descriptions-item>
            <el-descriptions-item label="状态"><span :class="getClass(detail.status)">{{ detail.status ? formatColumnMap(statusMap, 'value', detail.status, 'desc') : '' }}</span></el-descriptions-item>
            <el-descriptions-item label="调拨车辆">{{ detail.truckCode }}</el-descriptions-item>
            <el-descriptions-item label="司机">{{ detail.driverName }}</el-descriptions-item>
            <el-descriptions-item label="押运员">{{ detail.escortName }}</el-descriptions-item>
            <el-descriptions-item label="计划执行日期">{{ detail.executeDate ? parseTime(detail.executeDate, '{y}-{m}-{d}') : '' }}</el-descriptions-item>
            <el-descriptions-item label="调拨数量">{{ detail.bottleNum }}瓶</el-descriptions-item>
            <el-descriptions-item label="更新时间">{{ detail.modifyTime ? parseTime(detail.modifyTime, '{y}-{m}-{d} {h}:{i}:{s}') : '' }}</el-descriptions-item>
            <el-descriptions-item label="备注">{{ detail.remarks }}</el-descriptions-item>
            <el-descriptions-item v-if="detail.status === 4" label="实际完成日期">{{ detail.finishDate ? parseTime(detail.finishDate, '{y}-{m}-{d}') : '' }}</el-descriptions-item>
            <el-descriptions-item v-if="detail.status === 4" label="完成人">{{ detail.finishUser }}</el-descriptions-item>
            <el-descriptions-item v-if="detail.status === 5" label="作废人">{{ detail.cancelUser }}</el-descriptions-item>
            <el-descriptions-item v-if="detail.status === 5" label="作废时间">{{ detail.cancelDate ? parseTime(detail.cancelDate, '{y}-{m}-{d} {h}:{i}:{s}') : '' }}</el-descriptions-item>
          </el-descriptions>
          <div class="sub-title">调拨单列表</div>
          <el-table
            :data="tableData"
            style="width: 100%;"
          >
            <el-table-column
              prop="stockTransCode"
              label="调拨单号"
              width="130"
            >
              <template slot-scope="scope">
                <el-link @click="showAllotDetail(scope.row)">{{ scope.row.stockTransCode }}</el-link>
              </template>
            </el-table-column>
            <el-table-column
              prop="outStoreName"
              label="调出仓库"
              width="120"
              show-overflow-tooltip
            />
            <el-table-column
              prop="inStoreName"
              label="调入仓库"
              width="120"
              show-overflow-tooltip
            />
            <el-table-column
              prop="bottleNum"
              label="申请数量"
            />
            <el-table-column
              prop="expectDt"
              label="期望日期"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.expectDt ? parseTime(scope.row.expectDt, '{y}-{m}-{d}') : '' }}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="remark"
              label="备注"
              show-overflow-tooltip
            />
            <el-table-column
              prop="transStatus"
              label="状态"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.transStatus ? formatColumnMap(transStatusMap, 'value', scope.row.transStatus, 'desc') : '' }}</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 右侧 -->
        <div class="right">
          <div class="timeline-title">流转信息</div>
          <el-timeline>
            <el-timeline-item v-for="(item, index) in flowInfoData" :key="index" :timestamp="`${parseTime(item.createTime, '{y}-{m}-{d} {h}:{i}')} ${item.operator}`" placement="top">
              <el-card>
                <h4>{{ item.traceDesc }}</h4>
                <p>{{ `发生部门&nbsp;——&nbsp;${item.orgName}` }}</p>
              </el-card>
            </el-timeline-item>
          </el-timeline>
        </div>
      </div>
      <!-- 底部 -->
      <div class="demo-drawer__footer" style="padding: 0 10px 10px 0; text-align: right;">
        <div class="btn-group">
          <el-button class="btn" @click="visible = false"> 关闭 </el-button>
        </div>
      </div>
    </el-drawer>
    <stock-trans-detail
      ref="stockTransDetail"
      :store-map="storeMap"
      :truck-map="truckList"
      :driver-map="driverList"
      :escort-map="escortList"
      :trans-type-map="transTypeMap"
      :trans-status-map="transStatusMap"
      :stock-out-type-map="stockOutTypeMap"
      :stock-in-type-map="stockInTypeMap"
      :gd-status-map="gdStatusMap"
    />
  </div>
</template>

<script>
  import { stockTransPlanDetail, queryMap, commontraceList, selectOrgSNNoAndReType, selectOrgSNNo, stockTransWin } from '@/api'
  import stockTransDetail from '../../transferManage/components/stockTransDetail.vue'
  import moment from 'dayjs'
  import { mergeAsyncFunc } from '@/utils/mergeAsyncFunc'
  import { formatColumnMap, parseTime } from '@/utils'
  import { mapGetters } from 'vuex'

  export default {
    name: 'allotPlanDetail',
    components: { stockTransDetail },
    props: {
      statusMap: {
        type: Array,
        required: true
      },
      truckList: {
        type: Array,
        required: true
      },
      driverList: {
        type: Array,
        required: true
      },
      escortList: {
        type: Array,
        required: true
      }
    },
    data() {
      return {
        moment,
        formatColumnMap,
        parseTime,
        baseSrc: 'http://image.haoyunqi.com.cn/',
        visible: false,
        detail: {},
        tableData: [],
        flowInfoData: [], // 流转信息
        labelStyle: { 'white-space': 'nowrap' },
        contentStyle: { 'word-break': 'break-all' },
        storeMap: [],
        transTypeMap: [{ desc: '内部充装调拨', value: 2 }, { desc: '内部调拨', value: 3 }],
        transStatusMap: [
          { value: -1, desc: '已作废', key: 'INVALID' },
          { value: 1, desc: '待审核', key: 'UNREVIEWED' },
          { value: 2, desc: '已审核', key: 'VALID' },
          { value: 4, desc: '驳回', key: 'REJECT' },
          { value: 5, desc: '已出库', key: 'OUT' },
          { value: 6, desc: '已入库', key: 'IN' }
        ],
        stockOutTypeMap: [],
        stockInTypeMap: [],
        gdStatusMap: []
      }
    },
    computed: {
      ...mapGetters([
        'orgList', 'corpSettingData', 'menusArr'
      ])
    },
    created() {
    },
    methods: {
      async open(temp) {
        this.initStoreMap()
        return new Promise((resolve) => {
          this.resolve = resolve
          this.visible = true
          this.planCode = temp.planCode
          this.getDetail()
        })
      },
      async initStoreMap() {
        selectOrgSNNoAndReType({
          currentPage: 1,
          pageSize: 9999,
          reType: 4
        }).then((res) => {
          this.storeMap = res.data
          this.leftStoreMap = res.data
        })
        selectOrgSNNo({
          orgSNNo: '',
          reType: 4
        }).then(res => {
          const rightStoreMap = res.data.filter(item => {
            return item.reType === 1 || item.reType === 3 || item.reType === 6
          })
          this.rightStoreMap = rightStoreMap
          this.storageStoreMap = rightStoreMap
        })
        const [res4, res5, res6] = await Promise.all([
          mergeAsyncFunc(queryMap, { enumName: 'StockOutType' }),
          mergeAsyncFunc(queryMap, { enumName: 'StockOutType' }),
          stockTransWin()
        ])
        this.stockOutTypeMap = res4.data // 出库类型
        this.stockInTypeMap = res5.data // 入库类型
        this.gdStatusMap = JSON.parse(res6.data.gdStatusMap) // 商品状态枚举
      },
      async getDetail() {
        const res = await stockTransPlanDetail({ planCode: this.planCode })
        console.log(res.data)
        this.detail = { ...res.data }
        this.tableData = res.data.billList
        const params = {
          billCode: this.planCode,
          billType: 13
        }
        const res2 = await commontraceList(params)
        this.flowInfoData = res2.data
      },
      getType(status) {
        const statusName = status ? formatColumnMap(this.statusMap, 'value', status, 'desc') : ''
        if (statusName === '未开始') {
          return 'warning'
        }
        if (statusName === '进行中') {
          return ''
        }
        if (statusName === '已入库') {
          return 'success'
        }
        if (statusName === '已完成') {
          return 'success'
        }
        if (statusName === '已作废') {
          return 'danger'
        }
        return ''
      },
      getClass(status) {
        const statusName = status ? formatColumnMap(this.statusMap, 'value', status, 'desc') : ''
        if (statusName === '未开始') {
          return 'orange-text'
        }
        if (statusName === '进行中') {
          return 'green-text'
        }
        if (statusName === '已入库') {
          return 'green-text2'
        }
        if (statusName === '已完成') {
          return 'green-text2'
        }
        if (statusName === '已作废') {
          return 'red-text'
        }
        return ''
      },
      showAllotDetail(row) {
        this.$refs.stockTransDetail.open(row)
      }
    }
  }
</script>

<style lang="scss" scoped>
.orderDetail {
  /deep/.el-drawer {
    width: 80% !important;
  }
  .csmDetail {
    /deep/.el-drawer {
      width: 75% !important;
    }
  }
}
/deep/.el-drawer__header {
  padding: 15px 20px;
  margin-bottom: 0;
}
.center {
  display: flex;
  height: calc(100vh - 120px);
  padding: 0 20px;
  overflow-y: auto;
  // height: 700px;
  font-size: 14px;
  font-weight: 400;
  color: #333;
  text-align: left;
  .left {
    position: relative;
    width: 75%;
    height: 100%;
    padding-right: 40px;
    .sub-title {
      margin: 20px 0 10px 0;
      font-weight: 600;
      color: #38BDBD;
    }
  }

  .right {
    height: 100%;
    padding-left: 15px;
    border-left: 1px solid #e3e3e3;
    flex: 1;
    .timeline-title {
      width: 100%;
      padding: 8px;
      margin-bottom: 10px;
      color: white;
      background-color: #38BDBD;
      border-radius: 5px;
    }
    .el-timeline {
      margin-top: 20px;
    }
  }
}
.demo-drawer__footer {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: 55px;
  background: white;
  border: 2px solid #DCDFE6;
  .btn {
    margin-top: 10px;
    margin-left: 74%;
  }
}
.title {
  display: flex;
  padding: 20px 20px 0 20px;
  // margin-bottom: 20px;
  .title-text {
    line-height: 25px;
  }
  .tag {
    margin: 0 10px;
  }
}
.green-text {
  color: #38BDBD;
}
.green-text2 {
  color: #67C23A;
}
.orange-text {
  color: #E6A23C;
}
.red-text {
  color: #F56C6C;
}
</style>
